﻿using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Threading.Tasks;

namespace PSAMMatrixCloud.TransferSecret.LaneMutual
{
     public class LaneChannelAlive
    {
        private static Task<IScheduler>  schedulerTask;
        private static IScheduler scheduler;


        public static void Start() 
        {
            ISchedulerFactory factory = new StdSchedulerFactory();
            schedulerTask = factory.GetScheduler();
            scheduler = schedulerTask.Result;
            IJobDetail LaneChannelAliveJobDetai = JobBuilder.Create<LaneChannelAliveJob>()
                .WithIdentity("LaneChannelAliveJob", "LaneChannel")
                .Build();
            ITrigger LaneChannelAliveTrigger = TriggerBuilder.Create()
            .WithIdentity("LaneChannelAliveTrigger", "GenerateStatics")
            .StartNow()
            .WithSimpleSchedule(x => x
            .WithIntervalInSeconds(60)
            .RepeatForever())
            .Build(); ;

            scheduler.ScheduleJob(LaneChannelAliveJobDetai, LaneChannelAliveTrigger);
            scheduler.Start();
        }

    }


    public class LaneChannelAliveJob: IJob
    {
        /// <summary>
        /// 是否运行 true表示运行；false表示没运行
        /// </summary>
        private static bool IsExistRun = false;

        public Task Execute(IJobExecutionContext context)
        {
            if (IsExistRun)
            {
                return Task.Run(() =>
               {
                   Common.LogHelper.Debug($"检查车道会话正在运行");
               });
            }
            return Task.Run(() =>
            {
                IsExistRun = true;
                LaneChannelManager.KeepAliveAsync();
                IsExistRun = false;
            });
        }
    }
}
